<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>用户添加</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script>

        layui.use(['form', 'jquery','element'], function () {
            var form = layui.form;
            var $ = layui.$;
            var userduty;
            form.render();
            form.on('select(duty)', function (data) {
                var duty = $("#duty").val();
                var ruledata = selectRuleInfo();
                ruleOnload(ruledata);
                if (duty == 0) {
                    $("#cityId").attr("disabled", true);
                    $("#countyId").attr("disabled", true);
                    $('#checkbox').html("");
                    console.info(data.value);
                    $("#Mgr").hide();
                    $("#Check").show();
                    form.render();
                } else if (duty == 1){//地市
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", true);
                    $("#Check").show();
                    $('#checkbox').html("");
                    $("#Mgr").hide();
                    form.render();
                } else if (duty == 3){//项目
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", false);
                    $('#checkbox').html("");
                    $("#Check").hide();
                    $("#Mgr").show();
                    form.render();
                } else if(duty == 2){//区县
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", false);
                    $("#Check").hide();
                    $("#Mgr").hide();
                    form.render();
                }
                if(userduty == 1){
                	$("#cityId").attr("disabled", true);
                	form.render();
                } 
            });
            //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data3 = selectMgrInfo(data.value);
                appendMgr(data3);
            });
            //监听是否复制
            form.on('radio(cpmgr)', function(data){
            	if(data.value==0){
            		$('#checkbox').html("");
            		$("#copyinfo").hide();
            		form.render();
            	}else if(data.value==1){
            		$('#checkbox').html("");
            		$("#copyinfo").show();
            		form.render();
            	}
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value=\"\">全部</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
            	var data1 = selectMgrInfo(data.value);
                appendMgr(data1);
            });
           	$('#copy').on('click', function () {
           	var copyname = $.trim($("#copyname").val());
           	var copymsisdn = $.trim($("#copymsisdn").val());
           	var param = {
           		copymsisdn : copymsisdn,
           		copyname : copyname
           	};
           	var url = '/user/findUserBymsisdn';
                $.ajax({
                    type: 'get',
                    data: param,
                    url: url,
                    success: function (data) {
                    	var obj = data.data;
                    	if(data.code==0){
                    	var checkboxs = "";
                        var mgrId = obj.mgrId;
                        var checkbox = mgrId.split(",");
                        var mgrName = obj.mgrIdName.split(",");
                        if(obj.duty==3){
                        $("#cityId").val(obj.cityId);
                        var data3 = selectCountyId(obj.cityId);
                		appendCounty(data3);
                		if(obj.countyId=="" || obj.countyId==null){
                        	obj.countyId=""
                        }
                        $("#countyId").val(obj.countyId);
                        for (var i = 0; i < checkbox.length; i++) {
                            checkboxs += '<input type="checkbox" value="' + checkbox[i] + '" name="mgrId" id="mgrId"   title="' + mgrName[i] + '"checked=true>';
                        }
                        $('#checkbox').html(checkboxs);
                        }
                        form.render();
                    }else{
                    	layer.msg(data.msg);
                    }}
                    
                    ,
                error: function (data) {
                            layer.msg(data.msg);
                }
                })
           	});
           	
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
            	if(countyId == ""){
            		countyId = $("#cityId").val();
            	}
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
            //动态拼接项目
            function appendMgr(data) {
               var duty = $("#duty").val();
               var cpmgr =$("input[name='cpmgr']:checked").val();
               if(duty==3 && cpmgr==0) {
                    if (data.length >0) {
                        var checkboxs = '';
                        for (var i = 0; i < data.length; i++) {
                            checkboxs += '<input type="checkbox" value="' + data[i].id + '" name="mgrId"  title="' + data[i].name + '" />';
                        }
                        $('#checkbox').html(checkboxs);
                    } else {
                        var checkboxs = '<span>该区域下没有项目</span>';
                        $('#checkbox').html(checkboxs);
                    }
                }
                form.render();
            }
            
            function selectRuleInfo() {
                var ruledata;
                var url = '/user/findRule';
                var param = {
                	duty : $("#duty").val()
                }
                
                $.ajax({
                        type: 'post',
                        async: false,
                        data:param,
                        url: url,
                        success: function (json) {
                            ruledata = json.data;
                        }
                    }
                );
                return ruledata;
            }
			function selectduty() {
                var dutydata;
                var url = '/user/findduty';
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            dutydata = json.data;
                        }
                    }
                );
                return dutydata;
            }
			$(document).ready(function () {
				var user ;
				var url = '/user/findduty';
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            user = json.data;
                        }
                    }
                )
                userduty=user.duty;
                var cityId = user.cityId;
                var cityName = user.cityName;
				var option = '<option value=""></option>';
				var cityOption;
				if(userduty==0){
                    option += '<option value=\"0\">省公司管理员</option>';
                    option += ' <option value=\"1\">地市管理员</option>';
                    option += ' <option value=\"2\">区县管理员</option>';
                    option += '<option value=\"3\">项目管理员</option>';
                                
				}else if (userduty==1){
                     option +=  ' <option value=\"2\">区县管理员</option>';
                     option +=  '<option value=\"3\">项目管理员</option>';
                     cityOption = '<option value='+cityId+'>'+cityName+'</option>';
                     $("#cityId").html(cityOption); 
                     $("#cityId").attr("disabled", true);
                     var data2 = selectCountyId(cityId);
                		appendCounty(data2);
				}else if (userduty==2){
                      option +=          '<option value="3">项目管理员</option>';
				}
				$('#duty').html(option);
				form.render();
			});
			
            function ruleOnload( ruledata) {
                var rulecheckboxs = "";
                for (var i = 0; i < ruledata.length; i++) {
                    rulecheckboxs += '<input type="checkbox"  value="' + ruledata[i].roleId + '" name="roleId" title="' + ruledata[i].roleName + '">';
                }
                $('#rulecheckbox').html(rulecheckboxs);
                form.render();
            }
			
            $('#add').on('click', function () {
                var url = '/user/add';
                var mgrId = "";
                var roleId = "";

                $("input:checkbox[name='mgrId']:checked").each(function () {
                    mgrId += $(this).val() + ",";
                });
                $("input:checkbox[name='roleId']:checked").each(function () {
                    roleId += $(this).val() + ",";
                });
                var userName = $.trim($("#username").val());
                var password =$.trim($("#password").val());
                var msisdn = $.trim($("#msisdn").val());
                var duty = $("#duty").val();
                var cityId = $("#cityId").val();
                var countyId = $.trim($("#countyId").val());
                var isCheck = $("input[name='isCheck']:checked").val();
                if (isNull(userName)) {
                    layer.msg("请输入用户名");
                    $("#username").focus();
                } else if (testMsisdn(msisdn) || isNull(msisdn)) {
                    layer.msg("请输入合法的手机号码");
                    $("#msisdn").focus();
                } else if (testPassword(password) || isNull(password)) {
                    layer.msg("您输入的密码不符合规范");
                    $("#password").focus();
                } else if (duty == "") {
                    layer.msg("请选择用户级别");
                } else if (cityId == "" && duty > 0 && duty <4) {
                    layer.msg("请选择一个地市");
                }else if (duty == 2 && countyId == "") {
                    layer.msg("您至少需要选择一个区县");
                }  else if (duty == 3 && mgrId == "") {
                    layer.msg("您至少需要选择一个项目");
                }  else if(isNull(roleId)){
                    layer.msg("您至少需要选择一个权限");
                }else {
                    if (duty==0){
                        cityId="25";
                    }
                    var param = {
                        userName: userName,
                        msisdn: msisdn,
                        password: password,
                        duty: duty,
                        cityId: cityId,
                        countyId: countyId,
                        mgrId: mgrId,
                        roleId: roleId,
                        isCheck: isCheck
                    }
                    console.info(param);
                    console.info(duty == 2 + countyId == "");
                    $.ajax({
                        type: 'post',
                        data: param,
                        url: url,
                        beforeSend: function () {
                        },
                        success: function (json) {
                            if (json == 1) {
                                layui.use('layer', function () {
                                    layer.confirm('添加成功，是否返回', {icon: 3, title: '提示'}, function (index) {
                                        layer.close(index);
                                        location.href = "operatorManage.html";
                                    });
                                });
                            } else if (json == -1) {
                                layer.msg('您输入的手机号已经存在');
                            }
                            else {
                                layer.msg('信息有误');
                            }
                        },
                        complete: function () {

                        },
                        error: function (data) {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
  <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href=""> &nbsp;系统管理&nbsp; </a>
  <a href="operatorManage.html"> &nbsp;用户管理&nbsp; </a>
 <a href="operatorAdd.html"> &nbsp;用户添加&nbsp; </a>
</span>
        </div>
        <div class="content-box">
            <div class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户姓名</label>
                        <div class="layui-input-block">
                            <input type="text" id="username" name="username" lay-verify="required" placeholder="输入用户姓名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-block">
                            <input type="text" id="msisdn" name="msisdn" lay-verify="required" placeholder="输入手机号码"
                                   autocomplete="off" class="layui-input" maxlength="11">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-inline">
                            <input type="text" id="password" name="password" lay-verify="required" placeholder="输入密码"
                                   autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline">
                            <p style="color: red;font-size:6px">提示:密码数量至少为8位，并且必须包含数字，字母和特殊的字符，不能是简单的数字连续密码</p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">级别</label>
                        <div class="layui-input-block">
                            <select name="duty" lay-filter="duty" id="duty">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" id="Mgr" style="display: none">
                            <div class="layui-input-block" style="margin-left:0px;">
                            <input type="radio" name="cpmgr" value="0" title="自定义管辖项目" lay-filter="cpmgr" checked="">
                            <input type="radio" name="cpmgr" value="1" title="复制已有用户项目" lay-filter="cpmgr">
                        </div>
                    </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">归属地</label>
                        <div class="layui-input-block">
                            <select name="interest" lay-filter="aihao" disabled>
                                <option value="1"> 江苏</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" id="city">
                        <label class="layui-form-label">地市</label>
                        <div class="layui-input-block">
                            <select name="cityId" lay-filter="cityId" id="cityId">
                                <option value="">全部</option>
                                <option value="11">苏州</option>
                                <option value="12">淮安</option>
                                <option value="13">宿迁</option>
                                <option value="14">南京</option>
                                <option value="15">连云港</option>
                                <option value="16">徐州</option>
                                <option value="17">常州</option>
                                <option value="18">镇江</option>
                                <option value="19">无锡</option>
                                <option value="20">南通</option>
                                <option value="21">泰州</option>
                                <option value="22">盐城</option>
                                <option value="23">扬州</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" id="city">
                        <label class="layui-form-label">区县</label>
                        <div class="layui-input-block">
                            <select name="countyId" lay-filter="countyId" id="countyId">
									<option value="">全部</option>
								</select>
                        </div>
                    </div>
                    <br>
                    <br>
                    <div class="layui-form-item" id="copyinfo" style="display: none">
                    <div class="layui-inline">
                        <label class="layui-form-label">复制人姓名</label>
                        <div class="layui-input-block">
                            <input type="text" id="copyname" name="copyname" lay-verify="required" placeholder="输入用户姓名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-block">
                            <input type="text" id="copymsisdn" name="copymsisdn" lay-verify="required" placeholder="输入手机号码"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal" id="copy">复&nbsp;制</button>
                        </div>
                </div>
                 
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目</label>
                            <div class="layui-input-block" id="checkbox">
                            </div>
                        </div>
                    </div>
                </div>
            
                <div class="layui-form-item">
                    <div class="layui-inline" pane="">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block" id="rulecheckbox">

                        </div>
                    </div>
                </div>
                <div class="layui-form-item" id="Check" style="display: none">
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" >是否审核人</label>
                            <div class="layui-input-block">
                            <input type="radio" name="isCheck" value="0" title="是" >
                            <input type="radio" name="isCheck" value="1" title="否" checked="">
                            </div>
                        </div>
                    </div>
                <div class="layui-block" pane="">
                    <button class="layui-btn layui-btn-normal" id="add">新&nbsp;增</button>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>